package com.sc.nft.pool;


import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.WxMiniUserInfo;
import com.sc.nft.enums.SourceEnum;
import com.sc.nft.exception.GlobalException;

import java.util.Objects;

public class UserPool {
    private static final InheritableThreadLocal<UserInfo> userPool = new InheritableThreadLocal<UserInfo>();
    private static final InheritableThreadLocal<WxMiniUserInfo> wxMiniUserPool = new InheritableThreadLocal<WxMiniUserInfo>();

    public static void clear() {
        userPool.set(null);
        wxMiniUserPool.set(null);
        userPool.remove();
        wxMiniUserPool.remove();
    }

    public static UserInfo getUser() {
        return userPool.get();
    }


    public static void setUser(UserInfo user) {
        userPool.set(user);
    }

    public static WxMiniUserInfo getWxMiniUser() {
        return wxMiniUserPool.get();
    }


    public static void setWxUser(WxMiniUserInfo wxUser) {
        wxMiniUserPool.set(wxUser);
    }

    /**
     * 获取玛特用户id
     *
     * @return
     */
    public static Long getUserId() {
        Long userId = null;
        if (Objects.nonNull(UserPool.getUser())) {
            userId = UserPool.getUser().getId();
        } else {
            userId = UserPool.getWxMiniUser().getUserId();
        }
        return userId;
    }

    /**
     * 获取小程序用户Id
     *
     * @return
     */
    public static Long getWxMiniUserId() throws GlobalException {
        Long wxMiniUserId = null;
        if (Objects.nonNull(UserPool.getWxMiniUser())) {
            wxMiniUserId = UserPool.getWxMiniUser().getId();
        }
        return wxMiniUserId;
    }

    public static SourceEnum getSource() {
        SourceEnum sourceEnum = null;
        if (Objects.nonNull(UserPool.getUser())) {
            sourceEnum = SourceEnum.META_APP;
        } else {
            sourceEnum = SourceEnum.MINI_MALL_APP;
        }
        return sourceEnum;
    }
}
